package com.tianbing.adlottery.controller.hordeadmin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.IDeltaBusiness;
import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.ISystemDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.DeltaRelation;
import com.tianbing.adlottery.controller.user.AdminMySaleController;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UserSession;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class AdminMyHordeController extends MultiActionController {

	Logger logger = Logger.getLogger(AdminMySaleController.class.getName());

	private String myHordeRecommendStoreListView;

	private String myHordeRecommendStoreProductListView;

	private String myHordeRecommendStoreCouponListView;

	private String myHordeMessageListView;

	private String addStore2MyHordeSuccView;

	private String addStoreCoupon2MyHordeSuccView;

	private String addStoreProduct2MyHordeSuccView;

	private IUserBusiness userBusiness;

	private IDeltaBusiness deltaBusiness;

	private ISiteBusiness siteBusiness;

	private ISiteDao siteDao;

	private IUserDao userDao;

	private IContentDao contentDao;

	private ICategoryDao categoryDao;

	private SkinUtils skinUtils;

	private ISystemDao systemDao;

	public ModelAndView addRecommendStore2MyHorde(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// process wordtext parameter
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		if (ssid > 0) {
			siteBusiness.addHordeRecommendStore(auth.getManagedHordeSysId(), ssid);
		}
		return new ModelAndView(addStore2MyHordeSuccView);
	}

	public ModelAndView listMyHordeRecommendStore(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(myHordeRecommendStoreListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = auth.getManagedStoreSysId();
		// get site2content for this site
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		Map hordeRecommendStores = siteBusiness.getHordeRecommendStores(auth.getManagedHordeSysId(), -1, pgno, pgsize);

		List site2SiteList = (List) hordeRecommendStores.get("site2SiteList");
		Long totalCount = (Long) hordeRecommendStores.get("totalCount");

		List site2SiteGroupList = UtilDataGroup.makeGroups(site2SiteList, 0, 6);

		model.put("site2SiteGroupList", site2SiteGroupList);
		model = skinUtils.preparePageFooter(request, site2SiteList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listMyHordeRecommendStoreProduct(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(myHordeRecommendStoreProductListView);
		Map<String, Object> model = mv.getModel();
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		Map hordeRecommendStoreProducts = siteBusiness.getHordeRecommendStoreProducts(auth.getManagedHordeSysId(), -1,
				pgno, pgsize);

		List<DeltaRelation> site2StoreProductList = (List<DeltaRelation>) hordeRecommendStoreProducts
				.get("site2StoreProductList");
		Long totalCount = (Long) hordeRecommendStoreProducts.get("totalCount");

		model.put("site2StoreProductList", site2StoreProductList);
		model = skinUtils.preparePageFooter(request, site2StoreProductList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listMyHordeRecommendStoreCoupon(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(myHordeRecommendStoreCouponListView);
		Map<String, Object> model = mv.getModel();
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		Map hordeRecommendStoreCoupons = siteBusiness.getHordeRecommendStoreCoupons(auth.getManagedHordeSysId(), -1,
				pgno, pgsize);

		List<DeltaRelation> site2StoreCouponList = (List<DeltaRelation>) hordeRecommendStoreCoupons
				.get("site2StoreCouponList");
		Long totalCount = (Long) hordeRecommendStoreCoupons.get("totalCount");

		model.put("site2StoreCouponList", site2StoreCouponList);
		model = skinUtils.preparePageFooter(request, site2StoreCouponList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listMyHordeMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(myHordeMessageListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = auth.getManagedStoreSysId();
		// get site2content for this site
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");
		Integer[] relationIds = { IConstants.RelationID.S2C_IS_POSTEE_OF.getId() };
		Integer[] statusIds = {};
		Integer[] contentTypeIds = { IConstants.Type.CONTENT_MESSAGE.getId() };
		List site2ContentList = siteDao.getListSite2ContentBy(auth.getManagedHordeSysId(), relationIds, -1, statusIds,
				contentTypeIds, null, null, pgno, pgsize, null);
		Long totalCount = siteDao.getCountSite2ContentBy(auth.getManagedHordeSysId(), relationIds, -1, statusIds,
				contentTypeIds, null, null);
		List site2ContentGroupList = UtilDataGroup.makeGroups(site2ContentList, 0, 6);

		model.put("site2ContentGroupList", site2ContentGroupList);
		model = skinUtils.preparePageFooter(request, site2ContentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView addStoreProduct2MyHorde(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// process wordtext parameter
		long s2csid = ServletRequestUtils.getLongParameter(request, "s2csid", -1);
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		if (s2csid > 0) {
			siteBusiness.addHordeRecommendStoreProduct(auth.getManagedHordeSysId(), s2csid);
		}
		return new ModelAndView(addStoreProduct2MyHordeSuccView);
	}

	public ModelAndView addStoreCoupon2MyHorde(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// process wordtext parameter
		long s2csid = ServletRequestUtils.getLongParameter(request, "s2csid", -1);
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		if (s2csid > 0) {
			siteBusiness.addHordeRecommendStoreCoupon(auth.getManagedHordeSysId(), s2csid);
		}
		return new ModelAndView(addStoreCoupon2MyHordeSuccView);
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setMyHordeRecommendStoreListView(String myHordeRecommendStoreListView) {
		this.myHordeRecommendStoreListView = myHordeRecommendStoreListView;
	}

	public void setAddStore2MyHordeSuccView(String addStore2MyHordeSuccView) {
		this.addStore2MyHordeSuccView = addStore2MyHordeSuccView;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setSystemDao(ISystemDao systemDao) {
		this.systemDao = systemDao;
	}

	public void setMyHordeMessageListView(String myHordeMessageListView) {
		this.myHordeMessageListView = myHordeMessageListView;
	}

	public void setAddStoreCoupon2MyHordeSuccView(String addStoreCoupon2MyHordeSuccView) {
		this.addStoreCoupon2MyHordeSuccView = addStoreCoupon2MyHordeSuccView;
	}

	public void setAddStoreProduct2MyHordeSuccView(String addStoreProduct2MyHordeSuccView) {
		this.addStoreProduct2MyHordeSuccView = addStoreProduct2MyHordeSuccView;
	}

	public void setMyHordeRecommendStoreProductListView(String myHordeRecommendStoreProductListView) {
		this.myHordeRecommendStoreProductListView = myHordeRecommendStoreProductListView;
	}

	public void setMyHordeRecommendStoreCouponListView(String myHordeRecommendStoreCouponListView) {
		this.myHordeRecommendStoreCouponListView = myHordeRecommendStoreCouponListView;
	}

	public void setDeltaBusiness(IDeltaBusiness deltaBusiness) {
		this.deltaBusiness = deltaBusiness;
	}

}
